package com.crealth.handler;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crealth.ConstantDefine;
import com.crealth.db.ConsumerProductDb;
import com.crealth.db.LoanContractDb;
import com.crealth.db.PledgeDiYaDb;
import com.crealth.db.UserDb;
import com.crealth.memcached.ConsumerCached;
import com.crealth.memcached.UserCached;
import com.crealth.model.ConsumerInfoModel;
import com.crealth.model.ConsumerProductModel;
import com.crealth.model.LoanContractModel;
import com.crealth.model.PledgeDiYaModel;
import com.crealth.model.UserModel;
import com.crealth.msg.NetMessageData;
import com.crealth.utils.StringUtil;
import com.crealth.utils.Utils;

public class ConsumerProductHandler {
	private static Logger errorLog = Logger.getLogger("error_log");

	public static NetMessageData obtainAllProduct(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);		
		Timestamp monthStartstp = Utils.getCurrentMonthStartTimestamp();
		String strMonth = Utils.getCurrentYearMonth2();
		boolean needFilter = false;
		List<ConsumerProductModel> productls = null;
		
		if (user.getPosition() == ConstantDefine.Installmenter ) {
			String mortgagerName = user.getUserName();
			productls = ConsumerProductDb.selectConsumerProductByMortgagerName(mortgagerName);
		}
		else if (user.getPosition() == ConstantDefine.Sales_Man || 
				user.getPosition() == ConstantDefine.Group_Leader ) {
			needFilter = true;
			productls = ConsumerProductDb.selectConsumerProductByUserID(userID);
		}
		else if (user.getPosition() == ConstantDefine.Team_Manager) {
			needFilter = true;
			List<Integer> unlingIDs = UserDb.selectUnderlingIDs(userID);
			if (unlingIDs != null && unlingIDs.size() > 0) {
				unlingIDs.add(userID);
				String userIDs = StringUtil.mergeListIntegerToString(unlingIDs, ",");
				productls = ConsumerProductDb.selectConsumerProductByUserIDs(userIDs);
			}
		}
		else if (user.isTopCompany() || 
				user.getPosition() == ConstantDefine.Admin_Manager || 
				user.getPosition() == ConstantDefine.InstallDpmMGR) {
			productls = ConsumerProductDb.selectAllConsumerProduct();
		}
		else {
			int companyID = user.getCompanyID();
			productls = ConsumerProductDb.selectConsumerProductByCompanyID(companyID);
		}		
		if (productls == null || productls.size() == 0) {
			resJson.put("productNum", 0);
			resJson.put("resCode", 0);		
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}		
		
		List<ConsumerProductModel> showProductls = null;
		if (needFilter) {
			showProductls = new ArrayList<ConsumerProductModel>();
			for (int idx = 0; idx < productls.size(); ++idx) {
				ConsumerProductModel curProduct = productls.get(idx);
				Timestamp bankSignTm = curProduct.getBankSignTm();
				Timestamp extendLoanTm = curProduct.getExtendLoanTm();
				Timestamp brokerageTm = curProduct.getBrokerageTm();
				int status = curProduct.getFollowStatus();
				if (curProduct.getProductNo().startsWith(strMonth) || 
					(bankSignTm != null && bankSignTm.after(monthStartstp)) || 
					(extendLoanTm != null && extendLoanTm.after(monthStartstp)) || 
					(brokerageTm != null && brokerageTm.after(monthStartstp)) || 
						status == ConsumerProductModel.Status_ZBZL || 
						status == ConsumerProductModel.Status_Commit ||
						status == ConsumerProductModel.Status_DSY ) {
					showProductls.add(curProduct);
				}
			}
		} else {
			showProductls = productls;
		}
		
		String strContractIDs = "";
		for (ConsumerProductModel product : productls) {
			int contractID = product.getContractID();
			if (strContractIDs.length() == 0) {
				strContractIDs = contractID + "";
			}
			else {
				strContractIDs += "," + contractID;
			}
		}
		
		List<LoanContractModel> contractls = LoanContractDb.selectLoanContractByIDs(strContractIDs);
		JSONArray jsonArray = new JSONArray();
		int sendContractCnt = 50;
		for (int i = 0; i < contractls.size(); ++i) {
			LoanContractModel contract = contractls.get(i);
			jsonArray.add(contract.toJSONObject());
			if (jsonArray.size() == sendContractCnt || i == contractls.size()-1) {
				resJson.put("contractArr", jsonArray);
				resJson.put("contractNum", contractls.size());
				resJson.put("resCode", 0);
				sendMsg.setMsgBody(resJson.toJSONString());
				ResponseMsgQueue.addMsgToQueue(NetMessageData.Net_All_LoanContract, userID, sendMsg);
				
				if (jsonArray.size() == sendContractCnt) {
					jsonArray = new JSONArray();
					resJson = new JSONObject();
					sendMsg = new NetMessageData();
				}
			}
		}
		
		contractls = LoanContractDb.selectLoanContractNotProduct(strContractIDs);
		jsonArray = new JSONArray();
		for (int i = 0; i < contractls.size(); ++i) {
			LoanContractModel contract = contractls.get(i);
			jsonArray.add(contract.toJSONObject());
			if (jsonArray.size() == sendContractCnt || i == contractls.size()-1) {
				resJson.put("contractArr", jsonArray);
				resJson.put("contractNum", contractls.size());
				resJson.put("resCode", 0);
				sendMsg.setMsgBody(resJson.toJSONString());
				ResponseMsgQueue.addMsgToQueue(NetMessageData.Net_All_LoanContract, userID, sendMsg);
				
				if (jsonArray.size() == sendContractCnt) {
					jsonArray = new JSONArray();
					resJson = new JSONObject();
					sendMsg = new NetMessageData();
				}
			}
		}
		
		int cmd = revData.getMsgCmd();
		jsonArray = new JSONArray();
		int productNum = showProductls.size();
		int prebagNum = 100;
		for (int idx = 0; idx < productNum; ++idx) {
			ConsumerProductModel product = showProductls.get(idx);
			jsonArray.add(product.toJSONObject());
			
			if (jsonArray.size() == prebagNum || idx == productNum-1) {
				resJson.put("productNum", productNum);
				resJson.put("productArr", jsonArray);
				resJson.put("resCode", 0);
				sendMsg.setMsgBody(resJson.toJSONString());
				ResponseMsgQueue.addMsgToQueue(cmd, userID, sendMsg);
			}
			if (jsonArray.size() == prebagNum) {
				sendMsg = new NetMessageData();
				resJson = new JSONObject();
				jsonArray = new JSONArray();
			}
		}
		
		return null;
	}
	
	public static NetMessageData saveProductInfo(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		//UserModel user = UserCached.selectUserByID(userID);
		int consumerProductID = msgJson.getIntValue("consumerProductID");
		ConsumerProductModel productModel = ConsumerProductDb.selectConsumerProductByID(consumerProductID);
		if (productModel == null) {
			return null;
		}		
		productModel.setId(consumerProductID);
		productModel.setProductName("");
		if (msgJson.containsKey("productName")) {
			String productName = msgJson.getString("productName");
			productModel.setProductName(productName);
		}
		productModel.setBankSignTm(null);
		if (msgJson.containsKey("bankSignedTm")) {
			long bankSignedTm = msgJson.getLongValue("bankSignedTm");
			productModel.setBankSignTm(new Timestamp(bankSignedTm*1000));
		}
		productModel.setExtendLoanTm(null);
		if (msgJson.containsKey("bankfangkuanTm")) {
			long bankfangkuanTm = msgJson.getLongValue("bankfangkuanTm");
			productModel.setExtendLoanTm(new Timestamp(bankfangkuanTm*1000));
		}
		productModel.setBrokerageTm(null);
		if (msgJson.containsKey("shouyongTm")) {
			long shouyongTm = msgJson.getLongValue("shouyongTm");
			productModel.setBrokerageTm(new Timestamp(shouyongTm*1000));
		}
		
		String mortgagerName = null;
		productModel.setMortgagerName("");
		if (msgJson.containsKey("mortgagerName")) {
			mortgagerName = msgJson.getString("mortgagerName");
			productModel.setMortgagerName(mortgagerName);
		}
		productModel.setBankExtendLoan("");
		if (msgJson.containsKey("shenpiMoney")) {
			String shenpiMoney = msgJson.getString("shenpiMoney");
			productModel.setBankExtendLoan(shenpiMoney);
		}
		productModel.setCustomerServiceMemo("");
		if (msgJson.containsKey("memo")) {
			String memo = msgJson.getString("memo");
			productModel.setCustomerServiceMemo(memo);
		}
		if (msgJson.containsKey("djType")) {
			String djType = msgJson.getString("djType");
			productModel.setEarnestPaytype(djType);
		}
		int status = ConsumerProductModel.Status_ZBZL;		
		if (productModel.getBankSignTm() != null) {
			status = ConsumerProductModel.Status_Commit;
		}
		String bankExtendLoan = productModel.getBankExtendLoan();
		if (productModel.getExtendLoanTm() != null && 
				bankExtendLoan.length() > 4) {
			status = ConsumerProductModel.Status_DSY;
		}
		if (bankExtendLoan != null && bankExtendLoan.compareTo("0") == 0) {
			status = ConsumerProductModel.Status_Reject;
		}
		if (productModel.getBrokerageTm() != null) {
			status = ConsumerProductModel.Status_YSY;
		}		
		if (msgJson.containsKey("hasCancel") && msgJson.getInteger("hasCancel") == 1) {
			status = ConsumerProductModel.Status_Cancel;
		}
		
		productModel.setFollowStatus(status);
		Integer res = ConsumerProductDb.updateConsumerProductByID(productModel);
		if (res == null || res == 0) {
			resJson.put("resCode", ErrorCode.ERROR_SQL_UPDATE);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		resJson.put("consumerProductID", consumerProductID);
		resJson.put("productInfo", productModel.toJSONObject());
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		
		int belongUserID = productModel.getUserID();
		ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), belongUserID, sendMsg);
		UserModel belogUser = UserCached.selectUserByID(belongUserID);
		if (belogUser != null && belogUser.getLeaderID() > 0) {
			ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), belogUser.getLeaderID(), sendMsg);
		}		
		int companyID = productModel.getCompanyID();
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToMortgagerMgr(userID, companyID, sendMsg);
		if (mortgagerName != null && mortgagerName.length() > 0) {
			UserModel mortgater = UserDb.selectUserByName(mortgagerName);
			NetMessageData sendMsg2 = new NetMessageData();
			JSONObject resJson2 = new JSONObject();
			if (mortgater != null) {
				resJson2.put("consumerProductID", consumerProductID);
				resJson2.put("productInfo", productModel.toJSONObject());
				resJson2.put("resCode", 0);
				resJson2.put("opUser", userID);				
				int contractID = productModel.getContractID();
				LoanContractModel loanContract = LoanContractDb.selectLoanContractByID(contractID);
				if (loanContract != null) {
					resJson2.put("contractInfo", loanContract.toJSONObject() );
					int consumerID = loanContract.getConsumerInfoID();
					ConsumerInfoModel consumerInfo = ConsumerCached.selectConsumer(consumerID);
					if (consumerInfo != null) {
						resJson2.put("consumerInfo", consumerInfo.toJSONObject());
					}
				}
				sendMsg2.setMsgBody(resJson2.toJSONString());
				sendMsg2.setMsgCmd(revData.getMsgCmd());
				ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), mortgater.getId(), sendMsg2);
			}
			ResponseMsgQueue.sendMsgToMortgagerMgr(userID, companyID, sendMsg2);
		}
		
		return sendMsg;
	}
	
	public static NetMessageData addNewProduct(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		if (!msgJson.containsKey("contractID")) {
			errorLog.error("addNewProduct  not contain contractID");
			return null;
		}
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		
		int contractID = msgJson.getIntValue("contractID");
		LoanContractModel contract = LoanContractDb.selectLoanContractByID(contractID);
		ConsumerProductModel product = new ConsumerProductModel();
		UserModel salesMan = UserCached.selectUserByID(contract.getUserID());
		product.setCompanyID(salesMan.getBelongCompanyID());
		String productNo = contract.getContractNo();
		int existCnt = ConsumerProductDb.selectContractProductCnt(contractID);
		productNo += ("-" + existCnt);
		product.setProductNo(productNo);
		product.setContractID(contractID);
		product.setUserID(contract.getUserID());
		ConsumerProductDb.insertConsumerProduct(product);
		
		resJson.put("productInfo", product.toJSONObject());
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		
		int companyID = user.getCompanyID();
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		UserModel belongUser = UserCached.selectUserByID(contract.getUserID());		
		if (belongUser != null ) {
			ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), contract.getUserID(), sendMsg);
			if (belongUser.getLeaderID() > 0) {
				ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), belongUser.getLeaderID(), sendMsg);
			}
		}
		
		return sendMsg;
	}
	
	public static NetMessageData acceptProduct(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel product = ConsumerProductDb.selectConsumerProductByID(productID);
		if (product == null) {
			return null;
		}
		if (product.getAccept() == 1) {
			return null;
		}
		Integer res = ConsumerProductDb.updateConsumerProductAccept(productID, 1);
		if (res != null) {
			int contractID = product.getContractID();
			LoanContractModel contract = LoanContractDb.selectLoanContractByID(contractID);
			if (contract != null) {
				int consumerID = contract.getConsumerInfoID();
				ConsumerCached.updateConsumerStatusByID(consumerID, ConsumerInfoModel.Contract_Accepted);
				ConsumerCached.updateConsumerFlowFollowByID(consumerID, ConsumerInfoModel.Flow_AJ);
			}
		}
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		resJson.put("productID", productID);
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		
		ResponseMsgQueue.sendBranchCompanyMgr(userID, user.getCompanyID(), sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		
		
		return sendMsg;
	}
	
	public static NetMessageData modifyDiYaFollow(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel product = ConsumerProductDb.selectConsumerProductByID(productID);
		if (product == null) {
			return null;
		}
		
		PledgeDiYaModel diyaModel = new PledgeDiYaModel();
		diyaModel.parseFromJson(msgJson);		
		Integer res = null;
		PledgeDiYaModel existDiya = PledgeDiYaDb.selectPledgeDiYaByProductID(productID);
		if (existDiya == null) {
			res = PledgeDiYaDb.insertPledgeDiYa(diyaModel);
		}
		else {
			res = PledgeDiYaDb.updatePledgeDiYaByID(diyaModel);
		}
		if (res == null) {
			return null;
		}
		
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		resJson.put("productID", productID);
		resJson.put("diya", diyaModel.toJSONObject(null));
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		
		ResponseMsgQueue.sendBranchCompanyMgr(userID, user.getCompanyID(), sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		
		return sendMsg;
	}
	
	public static NetMessageData obtainDiYaFollowDetail(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		//int userID = msgJson.getIntValue("id");
		//UserModel user = UserCached.selectUserByID(userID);
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel product = ConsumerProductDb.selectConsumerProductByID(productID);
		if (product == null) {
			return null;
		}
		PledgeDiYaModel diyaModel = PledgeDiYaDb.selectPledgeDiYaByProductID(productID);
		
		resJson.put("resCode", 0);
		if (diyaModel != null) {
			resJson.put("diya", diyaModel.toJSONObject(null));
		}
		sendMsg.setMsgBody(resJson.toJSONString());
		
		return sendMsg;
	}
	
	public static NetMessageData financeConfirmEarnest(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() != ConstantDefine.Financer) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);		
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}		
		
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel existProduct = ConsumerProductDb.selectConsumerProductByID(productID);
		if (existProduct == null) {
			return null;
		}		
		
		String csMemo = msgJson.getString("csMemo");
		ConsumerProductDb.updateCustomerServiceMemoByID(productID, csMemo);		
		ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_YS);
		
		resJson.put("csMemo", csMemo);
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		resJson.put("productID", productID);
		sendMsg.setMsgBody(resJson.toJSONString());
		
		sendMsg.setMsgCmd(revData.getMsgCmd());
		int companyID = existProduct.getCompanyID();
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		int belongUserID = existProduct.getUserID();
		ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), belongUserID, sendMsg);
		UserModel belogUser = UserCached.selectUserByID(belongUserID);
		if (belogUser != null && belogUser.getLeaderID() > 0) {
			ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), belogUser.getLeaderID(), sendMsg);
		}
		ResponseMsgQueue.sendMsgToMortgager(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToMortgagerMgr(userID, companyID, sendMsg);
		
		return sendMsg;
	}
	
	//  财务 确认佣金
	public static NetMessageData financeConfirmBrokerage(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() != ConstantDefine.Financer) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);		
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel existProduct = ConsumerProductDb.selectConsumerProductByID(productID);
		if (existProduct == null) {
			return null;
		}
		String expenseValue = msgJson.getString("expenseValue");
		String earnestMoney = msgJson.getString("earnestMoney");
		int contractID = existProduct.getContractID();
		if (expenseValue.length() > 0 && earnestMoney.length() > 0) {
			Integer res = LoanContractDb.updateExpenseEarnestByID(contractID, expenseValue, earnestMoney);
			if (res != null && res > 0) {
				resJson.put("contractID", contractID);
				resJson.put("expenseValue", expenseValue);
				resJson.put("earnestMoney", earnestMoney);
			}
		}
		
		String brokerage = msgJson.getString("brokerage");
		String basePushMoney = msgJson.getString("basePushMoney");
		String extraPushMoney = msgJson.getString("extraPushMoney");
		String mortgagerPushMoney = msgJson.getString("mortgagerPushMoney");
		String memo = msgJson.getString("memo");
		int nbasePush = Integer.valueOf(basePushMoney);
		int nextraPush = Integer.valueOf(extraPushMoney);
		int nmortgagerPush = Integer.valueOf(mortgagerPushMoney);
		ConsumerProductModel product = new ConsumerProductModel();
		product.setId(productID);
		product.setBrokerage(brokerage);
		product.setBasePushMoney(nbasePush);
		product.setExtraPushMoney(nextraPush);
		product.setMortgagerPushMoney(nmortgagerPush);
		product.setCustomerServiceMemo(memo);
		ConsumerProductDb.updateProductBrokerage(product);
		int djYD = 0;
		if (msgJson.containsKey("djYD")) {
			djYD = msgJson.getIntValue("djYD");
		}
		if (djYD == 1) {
			ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_YD);
			product.setEarnestStatus(ConsumerProductModel.DJ_YD);			
			//  子产品   定金已抵    主产品定金已抵
			int spaceIdx = existProduct.getProductNo().indexOf("-");
			if (spaceIdx > 0) {
				String mainProductNo = existProduct.getProductNo().substring(0, spaceIdx);
				ConsumerProductModel mainProduct = ConsumerProductDb.selectConsumerProductByProductNo(mainProductNo);
				if (mainProduct != null) {
					Integer updateManRes = ConsumerProductDb.updateEarnestStatusByID(mainProduct.getId(), ConsumerProductModel.DJ_YD);
					if (updateManRes != null && updateManRes > 0) {
						resJson.put("mainProductDJ_YD", 1);
					}
				}
			}
		}		
		int djDZY = 0;
		if (msgJson.containsKey("djDZY")) {
			djDZY = msgJson.getIntValue("djDZY");
		}
		if (djDZY == 1) {
			ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_DZY);
			product.setEarnestStatus(ConsumerProductModel.DJ_DZY);
		}
		// 撤销   定金已抵    定转佣
		if (djYD == 0 && djDZY == 0) {
			if (existProduct.getEarnestStatus() == ConsumerProductModel.DJ_YD ||
				existProduct.getEarnestStatus() == ConsumerProductModel.DJ_DZY ) {
				ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_YS);
				product.setEarnestStatus(ConsumerProductModel.DJ_YS);
			}
		}
		
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		resJson.put("productInfo", product.toJSONObject());
		sendMsg.setMsgBody(resJson.toJSONString());
		
		sendMsg.setMsgCmd(revData.getMsgCmd());
		int companyID = existProduct.getCompanyID();
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		
		int belongUserID = existProduct.getUserID();
		ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), belongUserID, sendMsg);
		UserModel belogUser = UserCached.selectUserByID(belongUserID);
		if (belogUser != null && belogUser.getLeaderID() > 0) {
			ResponseMsgQueue.addMsgToQueue(revData.getMsgCmd(), belogUser.getLeaderID(), sendMsg);
		}
		ResponseMsgQueue.sendMsgToMortgager(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToMortgagerMgr(userID, companyID, sendMsg);
		
		return sendMsg;
	}
	
	//  删除 产品
	public static NetMessageData deleteProduct(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() != ConstantDefine.Admin_Manager) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);		
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		int productID = msgJson.getIntValue("productID");
		int contractID = msgJson.getIntValue("contractID");
		ConsumerProductModel product = ConsumerProductDb.selectConsumerProductByID(productID);
		Integer res = ConsumerProductDb.deleteProductByID(productID);
		if (res != null && res > 0 && product.getProductNo().indexOf("-") <= 0) {
			LoanContractDb.deleteContractByID(contractID);
			resJson.put("contractID", contractID);
		}
		resJson.put("resCode", 0);
		resJson.put("productID", productID);
		resJson.put("opUser", userID);
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		
		int companyID = user.getCompanyID();
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		
		return sendMsg;
	}
	
	
}
